<template>
  <div class="login container">
    <div class="middle-container">
      <div style="height: 1px; width: 0;"></div>
      <div class="welcome">欢迎登录</div>
      <input v-model="phoneNumber" class="input-item" />
      <div class="input-item">
        <input v-model="checkCode" class="inner-input" />
        <div class="checkCode" v-if="!sendingCode" @click="getSMS">获取验证码</div>
        <div class="checkCode" v-else>{{ countDown }}秒</div>
      </div>
      <div class="login-btn" @click="bindWechat">登 录</div>
      <div class="bottomTip">
        <img class="checkImg" :src="checked ? checkedImg : checkImg" @click="checked = !checked" />
        <div>请您仔细阅读并同意《用户协议》、《隐私政策》和《提现转账协议》</div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
// @ts-nocheck
import { ref, watch } from "vue";
import { requireImg } from "@/assets/imgUrl";
import { getSmsCode, bindWechatUser } from '@/api/login';
import { useRouter } from "vue-router";

export default {
  name: "App",
  setup() {
    const router = useRouter();


    return { router }
  },
  data() {
    return {
      checked: true,
      sendingCode: false,
      phoneNumber: '',
      countDown: 60,
      tempInterval: null,
      checkCode: '',
      checkImg: requireImg('login/check.png'),
      checkedImg: requireImg('login/checked.png')
    }
  },
  mounted() {

  },
  methods: {
    bindWechat() {
      if (!this.checkCode) {
        this.$showToast("短信验证码不能为空！");
        return;
      }

      bindWechatUser({
        "checkCode": this.checkCode,
        "openid": window.openId,
        "telphoneCode": this.phoneNumber
      }).then(res => {
        this.router.go(-1)
      })
    },
    getSMS() {
      console.log(this.phoneNumber)
      let phoneNumPatten = /^1\d{10}$/; //校验手机号
      if (!this.phoneNumber) {
        this.$showToast("手机号码不能为空！");
        return;
      }
      if (!this.phoneNumber.match(phoneNumPatten)) {
        this.$showToast("手机号码输入不正确！");
        return;
      }

      this.sendingCode = true;
      window.clearInterval(this.tempInterval);
      this.tempInterval = setInterval(() => {
        if (this.countDown == 0) {
          window.clearInterval(this.tempInterval);
          this.sendingCode = false;
          this.countDown = 60;
          return;
        } else {
          this.countDown -= 1;
        }
      }, 1000);

      getSmsCode({
        telphoneCode: this.phoneNumber,
      }).then(res => {
        this.$showToast('发送成功')
      })
    }
  }
};
</script>
<style lang="scss" scoped>
.login {
  background-image: url('@/assets/images/login/login-bg.png');
  width: 100vw;
  height: 56vw;
  background-size: 100% 100%;

  .middle-container {
    background: #FFFFFF;
    box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.08), 0px -2px 8px -2px rgba(0, 0, 0, 0.05);
    border-radius: 12px;
    position: relative;
    width: 94.8vw;
    left: 2.6vw;
    top: 50px;
    text-align: center;
    padding-bottom: 5vw;

    .welcome {
      margin-top: 32px;
      margin-bottom: 50px;
      font-size: 20px;
      font-weight: 500;
    }

    .input-item {
      background: #F6F6F6;
      box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.08), 0px -2px 8px -2px rgba(0, 0, 0, 0.05);
      border-radius: 100px;
      outline: none;
      border: 0;
      width: 315px;
      margin: auto;
      padding-left: 20px;
      font-weight: 500;
      height: 48px;
      display: flex;
      align-items: center;
      margin-bottom: 24px;

      .inner-input {
        outline: 0;
        border: 0;
        background-color: transparent;
      }

      .checkCode {
        color: blue;
      }
    }

    .login-btn {
      background: #4F6DF2;
      box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.08), 0px -2px 8px -2px rgba(0, 0, 0, 0.05);
      border-radius: 100px;
      width: 315px;
      height: 48px;
      margin: auto;
      line-height: 48px;
      color: white;
    }

    .bottomTip {
      display: flex;
      align-items: center;
      font-size: 12px;
      width: 315px;
      margin: auto;
      margin-top: 20vw;
      text-align: left;

      .checkImg {
        margin-right: 2vw;
        width: 20px;
      }
    }
  }
}
</style>